package com.bysj.lms.service.impl;

import com.bysj.lms.pojo.entity.Course;
import com.bysj.lms.pojo.entity.Room;
import com.bysj.lms.pojo.vo.CourseVo;
import com.bysj.lms.service.ICourseRangeService;
import com.bysj.lms.service.ICourseService;
import com.bysj.lms.service.IRoomService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
@Service
public class CourseRangeServiceImpl implements ICourseRangeService {

    @Autowired
    ICourseService courseService;
    @Autowired
    IRoomService roomService;

    @Override
    public List<CourseVo> getBlackWeek(Integer classId,String tname, String term,Integer startWeek,Integer endWeek, Integer week) {
        //获取班级课程列表
        List<Course> classList = courseService.lambdaQuery().eq(Course::getTerm, term).eq(Course::getClassId,classId).eq(Course::getWeek,week).list();
        //获取教师课程列表
        List<Course> teacherList = courseService.lambdaQuery().eq(Course::getTerm, term).eq(Course::getTeacher, tname).eq(Course::getWeek,week).list();
        //获取期望时间内有课的教室
        List<Course> roomList = courseService.lambdaQuery().eq(Course::getTerm, term).eq(Course::getWeek, week).le(Course::getEndWeek,endWeek).ge(Course::getStartWeek,startWeek).list();
        //获取教室列表
        List<Room> allRooms = roomService.list();
        //期望周次
        List<Integer> exp = IntStream.rangeClosed(startWeek, endWeek).boxed().collect(Collectors.toList());
        List<CourseVo> courseVos = new ArrayList<>();
        for(int i=1;i<=11;i++){
            int ii = i;
            CourseVo courseVo = new CourseVo();
            courseVo.setSection("第"+i+"节");
            courseVo.setWeek("星期"+week);
            courseVo.setBlackWeek(exp);
            //班级空闲
            List<Course> collect = classList.stream().filter(course -> course.getSection() == ii).collect(Collectors.toList());
            collect.forEach(course -> {
                List<Integer> occupy = new ArrayList<>();
                if(course.getType().equals("0")){
                   occupy = IntStream.rangeClosed(course.getStartWeek(), course.getEndWeek()).boxed().collect(Collectors.toList());
                }else {
                    String[] split = course.getWeekTime().split(",");
                    for(String s:split){occupy.add(Integer.parseInt(s));}
                }
                List<Integer> finalOccupy = occupy;
                List<Integer> reduce = courseVo.getBlackWeek().stream().filter(item -> !finalOccupy.contains(item)).collect(Collectors.toList());
                courseVo.setBlackWeek(reduce);
            });
            //教师空闲
            List<Integer> tReduce = new ArrayList<>();
            tReduce.addAll(exp);
            List<Course> collect1 = teacherList.stream().filter(course -> course.getSection() == ii).collect(Collectors.toList());
            collect1.forEach(course -> {
                List<Integer> occupy = new ArrayList<>();
                if(course.getType().equals("0")){
                    occupy = IntStream.rangeClosed(course.getStartWeek(), course.getEndWeek()).boxed().collect(Collectors.toList());
                }else{
                    String[] split = course.getWeekTime().split(",");
                    for(String s:split){occupy.add(Integer.parseInt(s));}
                }
                tReduce.removeAll(occupy);
            });
            //班级和教师空闲交集
            courseVo.getBlackWeek().retainAll(tReduce);
            //该节次空闲的教室
            List<Integer> rids = roomList.stream().filter(course -> course.getSection() == ii).map(t -> t.getRoomId()).collect(Collectors.toList());
            List<Room> blackRooms = allRooms.stream().filter(room -> !rids.contains(room.getId())).collect(Collectors.toList());
            courseVo.setBlackRoom(blackRooms);
            courseVos.add(courseVo);
        }
        return courseVos;
    }

//    @Override
//    public List<Room> getBlackRoom(String term, Integer startWeek, Integer endWeek, Integer week,Integer section) {
//        return null;
//    }


}
